package com.boss.core.manage;

//import java.io.IOException;
//import java.util.Arrays;
//import java.util.List;

import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.Stapler;

//import hudson.EnvVars;
import hudson.Extension;
//import hudson.Launcher;
//import hudson.model.BuildListener;
//import hudson.model.Environment;
//import hudson.model.AbstractBuild;
import hudson.model.ComputerSet;
import hudson.model.Node;
import hudson.slaves.NodeProperty;
import hudson.slaves.NodePropertyDescriptor;

/**
 * Node Property
 * @author Momin Noor Khan
 */
public class BossNodeProperty extends NodeProperty<Node> {
    /**
     * Slave-specific environment variables
     */
    //private final EnvVars envVars;
    private final Entry entry;

//    @DataBoundConstructor
//    public BossNodeProperty(List<Entry> env) {
//        this.envVars = toMap(env);
//    }

    @DataBoundConstructor
    public BossNodeProperty(Entry entry) {
        this.entry = entry;
    }

//    public BossNodeProperty(Entry... env) {
//        this(Arrays.asList(env));
//    }

//    public BossNode() {
//        this(Arrays.asList(null));
//    }

//    public EnvVars getEnvVars() {
//    	return envVars;
//    }

    public Entry getEntry() {
        return entry;
    }

//    @Override
//    public Environment setUp(AbstractBuild build, Launcher launcher,
//			BuildListener listener) throws IOException, InterruptedException {
//    	return Environment.create(envVars);
//    }

    @Extension
    public static class DescriptorImpl extends NodePropertyDescriptor {

        @Override
        public String getDisplayName() {
            return "Environment Variable";
        }

        public String getHelpPage() {
            // yes, I know this is a hack.
            ComputerSet object = Stapler.getCurrentRequest().findAncestorObject(ComputerSet.class);
            if (object != null) {
                // we're on a node configuration page, show show that help page
                return "/help/system-config/nodeEnvironmentVariables.html";
            } else {
                // show the help for the global config page
                return "/help/system-config/globalEnvironmentVariables.html";
            }
        }
    }

    public static class Entry {
        private String key;
        private String value;

        @DataBoundConstructor
        public Entry(String key, String value) {
            this.key = key;
            this.value = value;
        }

        public String getKey() {
            return key;
        }

        public String getValue() {
            return value;
        }
    }

//	private static EnvVars toMap(List<Entry> entries) {
//		EnvVars map = new EnvVars();
//        if (entries!=null)
//            for (Entry entry: entries)
//                map.put(entry.key,entry.value);
//		return map;
//	}
}
